package com.iotproject.eduorder.controller.admin;

import com.iotproject.commonutils.R;
import com.iotproject.eduorder.entity.CourseOrder;
import com.iotproject.eduorder.entity.vo.admin.OrderQuery;
import com.iotproject.eduorder.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/eduorder/order/")
public class AdminOrderController {

    @Autowired
    OrderService orderService;

    @PostMapping("pageOrderCondition/{current}/{limit}")
    public R pageOrderCondition(@PathVariable("current") long current,
                                  @PathVariable("limit") long limit,
                                  @RequestBody(required = false) OrderQuery orderQuery) {
        Page<CourseOrder> orderPage = new Page<>(current, limit);
        QueryWrapper<CourseOrder> orderQueryWrapper = new QueryWrapper<>();

        String orderNo = orderQuery.getOrderNo();
        String courseTitle = orderQuery.getCourseTitle();
        String teacherName = orderQuery.getTeacherName();
        String mobile = orderQuery.getMobile();
        String begin = orderQuery.getBegin();
        String end = orderQuery.getEnd();
        if (!StringUtils.isEmpty(courseTitle)) {
            orderQueryWrapper.like("course_title", courseTitle);
        }
        if (!StringUtils.isEmpty(teacherName)) {
            orderQueryWrapper.like("teacher_name", teacherName);
        }
        if (!StringUtils.isEmpty(mobile)) {
            orderQueryWrapper.eq("mobile", mobile);
        }
        if (!StringUtils.isEmpty(orderNo)) {
            orderQueryWrapper.eq("order_no", orderNo);
        }
        if (!StringUtils.isEmpty(begin)) {
            orderQueryWrapper.ge("gmt_modified", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            orderQueryWrapper.le("gmt_modified", end);
        }
        orderQueryWrapper.orderByDesc("gmt_modified");
        orderQueryWrapper.eq("status", 1);//查询已支付的订单
        orderService.page(orderPage, orderQueryWrapper);
        long total = orderPage.getTotal();
        List<CourseOrder> records = orderPage.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    @DeleteMapping("deleteOrderById/{id}")
    public R deleteOrderById(@PathVariable("id") String id) {
        boolean removeById = orderService.removeById(id);
        if (removeById) {
            return R.ok();
        }
        return R.error();
    }

}
